package com.caxxb.dao.impl;

import com.caxxb.dao.ShoppingCartDao;
import com.caxxb.entity.ShoppingCart;
import com.caxxb.util.DBUtil;
import com.caxxb.util.PageData;

import java.util.List;

public class ShoppingCartDaoImpl implements ShoppingCartDao {


    //通过用户id显示订单
    @Override
    public List getCartsByUserId(int userid) {
        String sql="select s.*,g.game_name,g.game_img from shopping_cart s INNER JOIN game g on s.game_id=g.game_id where s.user_id=?";
        List list = DBUtil.exQuery(sql, ShoppingCart.class, userid);
        return list;
        /*PageData pageData = DBUtil.exQueryByPage(sql, ShoppingCart.class, pageNo, pageSize, userid);
        return pageData;*/
    }

    //通过id查询是否存在购物车内
    @Override
    public List getCartById(int userid,int gameid) {
        String sql="select * from shopping_cart where user_id = ? and game_id =?";
        List list = DBUtil.exQuery(sql, ShoppingCart.class, userid,gameid);
        return list;
    }

    //加进购物车的方法
    @Override
    public boolean addToCart(int gameid, int userid, double gameprice) {

        String sql="insert into shopping_cart values(null,?,?,?)";
        int result = DBUtil.exUpdate(sql,gameid,userid,gameprice);
        return result>0;

    }

    //清空购物车的方法
    @Override
    public boolean delCartById(int userid, int gameid) {
        String sql="delete from shopping_cart where user_id =? and game_id =?";
        int result = DBUtil.exUpdate(sql,userid,gameid);
        return result>0;
    }

    //根据购物车id删除购物车的方法
    @Override
    public boolean delCartByCartId(int cartid) {
        String sql="delete from shopping_cart where cart_id =?";
        int result = DBUtil.exUpdate(sql,cartid);
        return result>0;
    }
}
